#!/usr/bin/env node
class Queue {
    constructor() {
      this.queue = [];
    }
  
    write(data) {
      return new Promise((resolve) => {
        this.queue.push(data);
        console.log('+ prd create:', data);
        resolve();
      });
    }
  
    read() {
      return new Promise((resolve) => {
        if (this.queue.length === 0) {
          resolve(null);
        } else {
          const data = this.queue.shift();
          console.log('- csm destroy:', data);
          resolve(data);
        }
      });
    }
  }
  
  class Producer {
    constructor(queue) {
      this.queue = queue;
    }
  
    create(data) {
      return this.queue.write(data);
    }
  }
  
  class Consumer {
    constructor(queue) {
      this.queue = queue;
    }
  
    destroy() {
      return this.queue.read();
    }
  }
  
  async function main() {
    const queue = new Queue();
    const producer = new Producer(queue);
    const consumer = new Consumer(queue);
  
    async function produce() {
      for (let i = 0; i < 5; i++) {
        await producer.create(i);
        await consumer.destroy();
      }
    }
  
    produce();
  }
  
  main();